﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace TemplateUpgate.Model
{
    public class Users
    {
        PhotocopyDataContext db = new PhotocopyDataContext();
        public bool checkLogin(string username, string password)
        {
            bool checkLogin = false;
            //Kiem tra dieu kien dang nhap
            User user = db.Users.Where(e => e.Username == username && e.Password == password).FirstOrDefault();
            if (user != null)
            {
                checkLogin = true;
            }
            return checkLogin;
        }

        public bool insertUser(string _username, string _password, DateTime _birthday, string _gender, string _CMND, string _address, string _telephone, string _email, string _name)
        {
            bool insertOK = false;
            User user = new User
            {
                Username = _username,
                Password = _password,
                Birthday = _birthday,
                Gender = _gender,
                CMND = _CMND,
                Address = _address,
                Telephone = _telephone,
                Email = _email,
                Name = _name
            };
            if (user != null)
            {
                db.Users.InsertOnSubmit(user);
                try
                {
                    db.SubmitChanges();
                    insertOK = true;
                }
                catch
                {

                }
            }
            return insertOK;
        }

        public bool updateUser(int _id, string _username, string _password, DateTime _birthday, string _gender, string _CMND, string _address, string _telephone, string _email, string _name)
        {
            bool updateOK = false;
            var query = from user in db.Users
                        where user.UserID == _id
                        select user;
            foreach (User user in query)
            {
                if (!string.IsNullOrEmpty(_username))
                {
                    user.Username = _username;
                }
                if (!string.IsNullOrEmpty(_password))
                {
                    user.Password = _password;
                }
                if (_birthday != null)
                {
                    user.Birthday = _birthday;
                }
                if (string.IsNullOrEmpty(_gender))
                {
                    user.Gender = _gender;
                }
                if (string.IsNullOrEmpty(_CMND))
                {
                    user.CMND = _CMND;
                }
                if (string.IsNullOrEmpty(_address))
                {
                    user.Address = _address;
                }
                if (string.IsNullOrEmpty(_telephone))
                {
                    user.Telephone = _telephone;
                }
                if (string.IsNullOrEmpty(_email))
                {
                    user.Email = _email;
                }
                if (string.IsNullOrEmpty(_name))
                {
                    user.Name = _name;
                }
            }

            try
            {
                db.SubmitChanges();
                updateOK = true;
            }
            catch
            {
                updateOK = false;
            }

            return updateOK;
        }

    }
}